package com.appspot.diasporajava.federation;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pegdown.Extensions;
import org.pegdown.LinkRenderer;
import org.pegdown.PegDownProcessor;
import org.pegdown.ToHtmlSerializer;
import org.pegdown.ast.RootNode;
import org.tautua.markdownpapers.Markdown;
import org.tautua.markdownpapers.parser.ParseException;

import com.appspot.diasporajava.controller.MainController;
import com.petebevin.markdown.MarkdownProcessor;

public class MarkDown {
	private static Log log = LogFactory.getLog(MarkDown.class);
	
	private static MarkdownProcessor m = new MarkdownProcessor();
	
	
	public static String transform(String string){
		string=string.replace("a href", "img src");
		string=string.replace("</a>", "");
		string=string.replace("#", "&#035;");
		string=string.replace("![](", "");
		string=string.replace("![Alt text](", "");
		return transformPegDown(string);
	}
	
	private static String transformPapers(String string) {
		Reader in = new StringReader(string);
		StringWriter out = new StringWriter();

		Markdown md = new Markdown();
		try {
			md.transform(in, out);
			return out.getBuffer().toString();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	private static String transformMarkdownJ(String string) {
		string.replaceAll("#", "&#35;");
//		if (string.lastIndexOf("![t]") > 0 || string.lastIndexOf("![]") > 0) {
//			string = transformNestedImages(string);
//		}

		// String result=string;
		String result = m.markdown(string);

		result = result.replaceAll("<a href=", "");
		result = result.replaceAll("<h1>", "#");
		result = result.replaceAll("</h1>", "");

		log.debug(result);

		return result;
	}

	private static String transformPegDown(String string) {
		PegDownProcessor processor = new PegDownProcessor(Extensions.ALL);
		// return processor.markdownToHtml(string);
		LinkRenderer linkRenderer = new LinkRenderer();

		RootNode astRoot = processor.parseMarkdown(string.toCharArray());
		ToHtmlSerializer serializer = new ToHtmlSerializer(linkRenderer);
		String result = serializer.toHtml(astRoot);
		
		return result;
		
//		if (result.lastIndexOf("![t]") > 0 || result.lastIndexOf("![]") > 0 || result.lastIndexOf("![Alt text]") > 0) {
//			return transformNestedImages(result);
//		} else {
//			return result;
//		}
	}

	private static String transformNestedImages(String string) {
		StringBuffer stringBuffer = new StringBuffer();
		String[] images = string.split("\\[ !\\[.*\\]");
		for (String image : images) {
			String[] pieces = image.split("\"");
			if (pieces.length == 1)
				stringBuffer.append(image);
			else if (pieces.length >= 4)
				stringBuffer.append("<a href='" + pieces[3] + "'><img src='" + pieces[1] + "'></img></a>");
			else
				stringBuffer.append("<a href='" + pieces[1] + "'><img src='" + pieces[1] + "'></img></a>");
		}
		return stringBuffer.toString();
	}
}
